package com.neusoft.nepm.service.impl;

import com.neusoft.nepm.dto.PageResponseDto;
import com.neusoft.nepm.dto.SvRemarksDto;
import com.neusoft.nepm.dto.SvpageRequestDto;
import com.neusoft.nepm.po.AqiFeedback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.nepm.mapper.SupervisorMapper;
import com.neusoft.nepm.po.Supervisor;
import com.neusoft.nepm.service.ISupervisorService;

import java.util.List;

@Service
public class SupervisorServiceImpl implements ISupervisorService{

	@Autowired
	private SupervisorMapper supervisorMapper;
	
	/****************** neps 工程 ********************/
	@Override
	public Supervisor getSupervisorByIdByPass(Supervisor supervisor) {
		return supervisorMapper.getSupervisorByIdByPass(supervisor);
	}
	
	@Override
	public int saveSupervisor(Supervisor supervisor) {
		return supervisorMapper.insert(supervisor);
	}
	
	@Override
	public Supervisor getSupervisorById(String telId) {
		return supervisorMapper.selectById(telId);
	}

	@Override
	public int resetPasswordById(String telId) {
		return supervisorMapper.resetPasswordById(telId);

	}

	@Override
	public PageResponseDto<Supervisor> listSupervisorPage(SvpageRequestDto request) {

		PageResponseDto<Supervisor> response = new PageResponseDto<>();

		//获取总行数
		int totalRow = supervisorMapper.getSupervisorCount(request);
		response.setTotalRow(totalRow);

		//如果总行数为0，那么直接返回
		if(totalRow==0) {
			return response;
		}

		//计算总页数
		int totalPageNum = 0;
		if(totalRow%request.getMaxPageNum()==0) {
			totalPageNum = totalRow/request.getMaxPageNum();
		}else {
			totalPageNum = totalRow/request.getMaxPageNum()+1;
		}
		response.setTotalPageNum(totalPageNum);

		//计算上一页和下一页
		int pageNum = request.getPageNum();
		response.setPreNum(pageNum);
		response.setNextNum(pageNum);
		if(pageNum>1) {
			response.setPreNum(pageNum-1);
		}
		if(pageNum<totalPageNum) {
			response.setNextNum(pageNum+1);
		}

		//计算开始查询记录数
		request.setBeginNum((pageNum-1)*request.getMaxPageNum());

		//查询业务数据
		List<Supervisor> list = supervisorMapper.listSupervisorPage(request);
		//给返回值填充余下数据
		response.setPageNum(pageNum);
		response.setMaxPageNum(request.getMaxPageNum());
		response.setList(list);

		System.out.println(totalRow);
		System.out.println(list.size());


		return response;
	}

	@Override
	public String addRemarksById(SvRemarksDto svRemarksDto) {
		System.out.println(svRemarksDto.getTelId()+"+"+svRemarksDto.getRemarks());
		supervisorMapper.addRemarksById(svRemarksDto);
		return "num";
	}

	@Override
	public void updateSupervisor(Supervisor supervisor) {
		supervisorMapper.updateById(supervisor);
	}

	/****************** neps 工程 ********************/
}
